package com.example.jpushdemo.util;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class PushExample {
	protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);

	// 指定推送
	protected static final String APP_KEY = "e3b3ebe43bc996e73648a64e";
	protected static final String MASTER_SECRET = "0f7507945e6a4c1561d8f8ec";
	public static final String TITLE = "日本专车";// 标题

	// 群发推送
	protected static final String GROUP_PUSH_KEY = "";
	protected static final String GROUP_MASTER_SECRET = "";
	public static final String MSG_CONTENT = "日本专车"; // 消息内容
	public static final String ALERT = "日本专车";// 推送内容

	// 占时没用
	// public static final String REGISTRATION_ID = "";
	// public static final String TAG = "";
	// public static long sendCount = 0;
	private static long sendTotalTime = 60 * 60 * 24; // 有效时间为一天、默认就是一天

	/** 推送接口 ---- 参数 alias表示别名 -----alert 表示推送内容 */
	public static void AppSendPush(String alias, String alert, String shopName, String type) {
		ClientConfig clientConfig = ClientConfig.getInstance();
		final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
		 PushPayload payload = buildPushObject_all_alert(alert);//群推
		//PushPayload payload = buildPushObject_alias_followUser(alias, alert, shopName, type);
		try {
			PushResult result = jpushClient.sendPush(payload);
			// 日志
			LOG.info("得到了结果 - " + result);
		} catch (APIConnectionException e) {
			LOG.error("连接错误,应该稍后重试!", e);
			LOG.error("Sendno: " + payload.getSendno());
		} catch (APIRequestException e) {
			LOG.error("从JPush服务器错误响应,应该检查并修复它! ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
			LOG.error("Sendno: " + payload.getSendno());
		}
	}

	/** 按别名推送 推送平台为IOS 和  Android */
	public static PushPayload buildPushObject_alias_followUser(String alias, String alert, String shopName, String type) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("title", "医院");
		map.put("body", alert);
		return PushPayload.newBuilder().setPlatform(Platform.android_ios())// 指定平台（这里是安卓和ios）
				.setAudience(Audience.alias(alias))// 指定别名
				.setNotification(Notification.newBuilder()
						/* ----Android---- */
						.addPlatformNotification(AndroidNotification.newBuilder()
								.setTitle(TITLE) // 标题安卓这边可以用该参数替换应用的名称
								.setAlert(alert) // 内容
								.addExtra("type", type) // 附加字段
								.build())
						 /* ----iOS---- */
						.addPlatformNotification(IosNotification.newBuilder()
								//.setBadge(1) // 该参数是用于控制iOS端角标自动增加的，不设置这默认每次推送加一，如果设置了则每次推送都只展示指定数字
								.setSound("happy") // 控制iOS提示音
								.setContentAvailable(true) // 推送唤醒
								.setMutableContent(true) // 通知扩展
								.setAlert(IosAlert.newBuilder().setTitleAndBody("", shopName, alert).build()) // 设置标题title、副标题subtitle、内容
								.addExtra("type", type) // 附加字段
								.build())
						.build())
				.setOptions(Options.newBuilder()
						.setApnsProduction(true)// true-推送生产环境（上线使用）、false-推送开发环境（测试使用参数）
						.setTimeToLive(sendTotalTime)
						.build())
				.build();
	}

	/** 推送所有 */
	public static PushPayload buildPushObject_all_alert(String alert) {
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all())
				.setNotification(Notification.alert(alert))
				.setOptions(Options.newBuilder()
						.setApnsProduction(true)// true-推送生产环境（上线使用）、false-推送开发环境（测试使用参数）
						.build())
						.build();
	}
	
	
//	public static void main(String[] args) {
//		AppSendPush("", "万泰你好", "");
//	}

}
